/**
 * Creates new Airpodder.ui.EpisodeGrid
 * @constructor
 * @param {Object} config A config object
 */
Airpodder.ui.EpisodeGrid = Ext.extend(Ext.grid.GridPanel, {
  initComponent:function() {
    var config = {
      store             : Airpodder.data.episodes,
      enableColumnMove  : false,
      stripeRows        : true,
      autoScroll        : true,
      columns           : [
        {header: "id",          sortable: false, width: 20,   dataIndex: 'id',          hidden: true},
        {header: "Status",      sortable: false, width: 32,   dataIndex: 'state'},
        {header: "Title",       sortable: false, width: 180,  dataIndex: 'title',       id: 'title',   renderer: function (val, x, store) {return '<b>' + val + '</b><br />' + store.data.description}},
        {header: "Size",        sortable: false, width: 80,   dataIndex: 'length',      renderer: Ext.util.Format.fileSize},
        {header: "Released",    sortable: false, width: 80,   dataIndex: 'pubDate',     renderer: Ext.util.Format.dateRenderer('m/d/Y')},
        {header: "description", sortable: false, width: 180,  dataIndex: 'description', hidden: true}
      ],
      autoExpandColumn  : 'title',
      sm                : new Ext.grid.RowSelectionModel({
        singleSelect:true
      })
    };
    Ext.apply(this, Ext.apply(this.initialConfig, config));
    Airpodder.ui.EpisodeGrid.superclass.initComponent.apply(this, arguments);

    this.on('rowcontextmenu', this.onRowContextMenu, this);
  }
  
  /**
   * Отображаем контекстное меню в гриде с эпизодами.
   * @param Grid            grid
   * @param Number          rowindex
   * @param Ext.EventObject e
   */
  , onRowContextMenu : function(grid, index, e){
    e.stopEvent();

    if (!this.rowContextMenu) {
      this.rowContextMenu = new Ext.menu.Menu({
        items : [{
          text    : 'Download',
          scope   : this,
          iconCls : 'x-download-icon',
          handler : function() {
            var grid  = Ext.getCmp('downloader_grid'),
                store = grid.getStore(), 
                dt    = this.ctxRecord.data;

            if (Ext.isEmpty(store.getById(dt.id)) == false)
            {
              return false;
            }
            store.insert(
              store.getCount(), 
              new Airpodder.data.Downloader({
                  id        : dt.id,
                  episode   : dt.title,
                  progress  : 0,
                  size      : dt.length,
                  speed     : 0,
                  status    : 'Waiting'
              }, dt.id)
            );
            Ext.getCmp('downloads_tab').setTitle('Downloads' + ' (' + store.getCount() + ')');
            Airpodder.util.downloader.downloadEpisode(dt.id, dt.url, dt.filename);
          }
        }]
      });
      this.rowContextMenu.on('hide', this.onRowContextMenuHide, this);
    }

    this.selModel.selectRow(index);

    if (this.ctxRow) {
      Ext.fly(this.ctxRow).removeClass('x-node-ctx');
      this.ctxRow = null;
    }
    this.ctxRow     = this.view.getRow(index);
    this.ctxRecord  = this.store.getAt(index);
    Ext.fly(this.ctxRow).addClass('x-node-ctx');
    this.rowContextMenu.showAt(e.getXY());
  }

  /**
   * Скрытие контекстного меню. Очищаем переменные, чтобы они не занимали память
   */
  , onRowContextMenuHide : function(){
    if (this.ctxRow) {
      Ext.fly(this.ctxRow).removeClass('x-node-ctx');
      this.ctxRow = null;
    }
  }
  
  , onRender:function() {
    this.store.load({params: {
      where: 'where channel_id = ?',
      args: ['397966625KZYM']
    }});
    Airpodder.ui.EpisodeGrid.superclass.onRender.apply(this, arguments);
  },
  
  showForChannel:function(channel_id) {
    this.store.load({params: {
        where: 'where channel_id = ?',
        args: [channel_id]
      }});
  }
});
 
Ext.reg('episodegrid', Airpodder.ui.EpisodeGrid); 
